<template>
  <div class="app-container">
    <div class="table-page-search-wrapper">
      <el-form :inline="true" label-width="100px">
        <el-row :gutter="18">
          <el-col :md="6" :sm="24">
            <el-form-item label="入库单号">
              <el-input
                v-model="queryParam.acceptanceNum"
                placeholder="请输入入库单号 "
                clearable
                @keyup.enter.native="onSearch"
                @clear="onSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="物料名称">
              <el-input
                v-model="queryParam.goodsName"
                placeholder="请输入物料名称"
                clearable
                @keyup.enter.native="onSearch"
                @clear="onSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <el-button
                v-waves
                type="primary"
                icon="el-icon-search"
                @click="onSearch"
              >
                查询
              </el-button>
              <el-button type="info" icon="el-icon-refresh" @click="reset">
                重置
              </el-button>
            </span>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table-operator">
      <el-button
        v-permission="['create']"
        type="primary"
        @click="$refs.form.open(undefined)"
      >
        新建入库
      </el-button>
    </div>

    <el-table
      v-loading="loadingData"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
      row-key="id"
      @sort-change="appendSorterParam"
    >
      <el-table-column align="center" label="入库单号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.acceptanceNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="物料名称">
        <template slot-scope="scope">
          <span>
            {{ scope.row.goodsName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="批号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.batchNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="规格">
        <template slot-scope="scope">
          <span>
            {{ scope.row.specifications }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入库总数量">
        <template slot-scope="scope">
          <span>
            {{ scope.row.quantity }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入库时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入库类型">
        <template slot-scope="scope">
          <span>{{ scope.row.receiptTypeLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入库人">
        <template slot-scope="scope">
          <span>
            {{ scope.row.createByName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="240"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-popconfirm
            v-if="row.receiptType != 'CXRK'"
            title="确定撤销入库吗？"
            @confirm="uninsert(row)"
          >
            <el-button slot="reference" v-permission="['update']" type="text">
              撤销入库
            </el-button>
          </el-popconfirm>
          <span
            v-if="row.receiptType != 'CXRK'"
            v-permission="['detail']"
            v-permission-again="['update', 'delete']"
          >
            <el-divider direction="vertical" />
          </span>
          <el-button
            v-if="row.receiptType != 'CXRK'"
            v-permission="['update']"
            type="text"
            @click="$refs.form.open(row.acceptanceNum)"
          >
            详情
          </el-button>
          <span
            v-if="row.receiptType != 'CXRK'"
            v-permission="['detail']"
            v-permission-again="['update', 'delete']"
          >
            <el-divider direction="vertical" />
          </span>
          <el-button
            v-permission="['update']"
            type="text"
            @click="remove(row.id)"
          >
            删除
          </el-button>
          <span
            v-permission="['detail']"
            v-permission-again="['update', 'delete']"
          >
            <el-divider direction="vertical" />
          </span>
          <el-button v-permission="['detail']" type="text" @click="showQr(row)">
            二维码
            <!-- <span>
              <img
                src="@/assets/QRCodeBlue.svg"
                style="width: 20px;height: 20px;"
                align="absmiddle"
              ></span> -->
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total > 0"
      :total="pagination.total"
      :page.sync="pagination.current"
      :limit.sync="pagination.pageSize"
      :style="{ textAlign: 'right' }"
      @pagination="handlePaginationChanged"
    />

    <diboot-form ref="form" @complete="getList" />
    <qr-code ref="qrCode" width="26%" title="入库二维码" />
  </div>
</template>

<script>
import list from '@/components/diboot/mixins/list'
import dibootForm from './form'
import waves from '@/directive/waves'
import qrCode from '@/components/QRCode'
import { dibootApi } from '@/utils/request'
export default {
  name: 'InstockList',
  directives: { waves },
  components: {
    dibootForm,
    qrCode
  },
  mixins: [list],
  data() {
    return {
      baseApi: '/instock',
      getListFromMixin: true,
      attachMoreList: [
        {
          target: 'Goods',
          label: 'goodsName',
          value: 'id'
        }
      ]
    }
  },
  methods: {
    uninsert(row) {
      const { id, goodsId } = row
      dibootApi
        .post('instock/unInsert', { id, goodsId, receiptType: 'CXRK' })
        .then(result => {
          console.log(result)
          if (result.code === 0) {
            this.$message({
              message: result.msg,
              type: 'success'
            })
          } else {
            this.$message({
              message: result.msg,
              type: 'error'
            })
          }
          this.getList()
        })
        .catch(err => {
          console.log(err)
        })
    },
    showQr(row) {
      const qrString = JSON.stringify({
        acceptanceNum: row.acceptanceNum,
        QRCode: 'Warehousing'
      })
      this.$refs.qrCode.open(row.acceptanceNum, qrString)
    }
  }
}
</script>
<style lang="scss" scoped></style>
